#!/bin/bash
# sh ./test_sh_notification.sh
:<<!
测试企业微信的通知发送
!

# 定义颜色常量
NC="\033[0m" # No Color
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
PURPLE="\033[0;35m"
CYAN="\033[0;36m"



CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"

function log_title() {
    echo "${PURPLE}------------------ $1 ------------------${NC}"
}

# 延迟20毫秒的函数
function delay() {
  local delay=1
  sleep "$delay"
}

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
#CommonFun_HomeDir_Absolute2=${CurrentDIR_Script_Absolute}/..
CommonFun_HomeDir_Absolute2=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..
CommonFun_HomeDir_Absolute=${CommonFun_HomeDir_Absolute2%/*}

Example_HOME_DIR_Absolute=${CommonFun_HomeDir_Absolute%/*}/example_packing_info

qscript_path_get_filepath="${CommonFun_HomeDir_Absolute}/qscript_path_get.sh"
qbase_update_json_file_singleString_script_path="$(sh ${qscript_path_get_filepath} qbase update_json_file_singleString)"
qbase_notification2wechat_path="$(sh ${qscript_path_get_filepath} qbase notification2wechat)"


test_branchInfo_dir="${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/test"
    

TEST_ROBOT_URL="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=925776da-1ff4-417a-922a-d5ced384050e"

TESTDATA_FILE_PATH="${CurrentDIR_Script_Absolute}/test_data_notification.json"

log_title "4"
 

shouldTest_UseBranchInfoTest="true"
if [ "${shouldTest_UseBranchInfoTest}" == "true" ]; then
    # 获取分支信息
    BRANCH_TEST_DATA_FILE_PATH="${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/test/test_data_branch_info.json"
    BRANCH_TEST_SCRIPT_FILE_PATH="${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/test/tssh_branch_detail_info_result.sh"
    sh "${BRANCH_TEST_SCRIPT_FILE_PATH}"
    CONTENT=$(cat $BRANCH_TEST_DATA_FILE_PATH | jq -r '.branch_info_result.Notification.current.full')
    # CONTENT="123"
    # 发送通知
    echo "${YELLOW}正在执行命令：《 ${BLUE}sh ${qbase_notification2wechat_path} -robot \"${TEST_ROBOT_URL}\" -content \"${CONTENT}\" -msgtype \"markdown\" ${YELLOW}》${NC}"
    sh ${qbase_notification2wechat_path} -robot "${TEST_ROBOT_URL}" -content "${CONTENT}" -msgtype "markdown"
fi




function shouldTest_app() {
    # 减少干扰的文件信息
    echo "{}" > ${TESTDATA_FILE_PATH} #清空文件内容,但清空成{}

    # 1、获取当前分支所包含的分支及其分支信息
    Package_Environment_FILE_PATH="${test_branchInfo_dir}/data/test_data_env_info.json"

    History_Versions_FILE_PATH="${test_branchInfo_dir}/data/test_data_history_version.json"
    # echo "History_Versions_FILE_PAT=${History_Versions_FILE_PATH}"
    sh "${qbase_update_json_file_singleString_script_path}" -jsonF "${Package_Environment_FILE_PATH}" -k "historyVersionJsonFile" -v "${History_Versions_FILE_PATH}"
    if [ $? != 0 ]; then
        return 1
    fi

    REBASE_BRANCH="origin/develop"
    App_Feature_Brances_Directory_PATH="${Example_HOME_DIR_Absolute}/featureBrances"
    if [ ! -d "${App_Feature_Brances_Directory_PATH}" ]; then
        echo "❌Error:文件夹不存在，请检查 ${Example_HOME_DIR_Absolute}/featureBrances"
        return 1
    fi
    Develop_Branchs_FILE_PATH="${test_branchInfo_dir}/data/test_data_branch_info.json"
    ignoreAddJsonFileNames=("dev_demo.json")
    ignoreCheckBranchNameArray=(master development dev_publish_out dev_publish_in dev_all)
    echo "正在执行命令：《 sh ${CommonFun_HomeDir_Absolute}/brances_info/all_branch_info_deal.sh -envInfoF \"${Package_Environment_FILE_PATH}\" -rebaseBranch \"${REBASE_BRANCH}\" -appBranchsDir \"${App_Feature_Brances_Directory_PATH}\" -branchInfoF \"${Develop_Branchs_FILE_PATH}\" -ignoreAddJsonFileNames \"${ignoreAddJsonFileNames}\" -ignoreCheckBranchs \"${ignoreCheckBranchNameArray[*]}\" -branchResultJsonF \"${TEST_DATA_RESULT_FILE_PATH}\" 》"
    sh ${CommonFun_HomeDir_Absolute}/brances_info/all_branch_info_deal.sh -envInfoF "${Package_Environment_FILE_PATH}" -rebaseBranch "${REBASE_BRANCH}" -appBranchsDir "${App_Feature_Brances_Directory_PATH}" -branchInfoF "${Develop_Branchs_FILE_PATH}" -ignoreAddJsonFileNames "${ignoreAddJsonFileNames}" -ignoreCheckBranchs "${ignoreCheckBranchNameArray[*]}" -branchResultJsonF "${TEST_DATA_RESULT_FILE_PATH}"
    if [ $? != 0 ]; then
        return 1
    fi

    echo "执行命令获取执行环境中指定为文件的指定信息类型(Pgyer升级信息\Notification通知信息)：\
    《sh ${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF \"${Package_Environment_FILE_PATH}\" --request-for-log-types \"${REQUEST_FOR_LOG_TYPE_ARRAY[*]}\" -comScriptHomeDir \"${CommonFun_HomeDir_Absolute}\" -resultSaveToJsonF \"${TESTDATA_FILE_PATH}\" 》"
    REQUEST_FOR_LOG_TYPE_ARRAY=("Notification" "Pgyer")
    sh ${CommonFun_HomeDir_Absolute}/brances_info/brances_info_log/brances_info_log.sh -envInfoF "${Package_Environment_FILE_PATH}" --request-for-log-types "${REQUEST_FOR_LOG_TYPE_ARRAY[*]}" -comScriptHomeDir "${CommonFun_HomeDir_Absolute}" -resultSaveToJsonF "${TESTDATA_FILE_PATH}"


    # 2、获取结果
    if [ $? != 0 ]; then # 失败
        CommonScriptResultJSONFilePath="${CommonFun_HomeDir_Absolute}/json/common_script_result.json"
        CONTENT=$(cat $CommonScriptResultJSONFilePath | jq -r '.package_message')
        cat ${CommonScriptResultJSONFilePath} | jq '.'
    else # 成功
        CONTENT=$(cat ${TESTDATA_FILE_PATH} | jq -r '.branch_info_result.Notification.all.text')
        cat ${TESTDATA_FILE_PATH} | jq '.'
    fi
    # echo "-------CONTENT=${CONTENT}"
    # 将结果发送通知
    sh ${qbase_notification2wechat_path} -robot "${TEST_ROBOT_URL}" -content "${CONTENT}" -msgtype "markdown"

    # 3、恢复文件内容   
    # echo "{}" > ${TESTDATA_FILE_PATH} #清空文件内容,但清空成{}

    # echo "\n\n"
}
# shouldTest_app
